package com.woniuxy.qiantai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.qiantai.entity.Book;
import com.woniuxy.qiantai.es.ESBookRepository;
import com.woniuxy.qiantai.mapper.BookMapper;
import com.woniuxy.qiantai.service.BookService;
import com.woniuxy.qiantai.vo.EsBook;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author woniumrwang
 * @since 2023-01-06 02:44:35
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    BookMapper bookMapper;

    @Autowired
    ESBookRepository esBookRepository;

    @Override
    public Page<Book> getPageBook(Integer currentPage, Integer pageSize){
        //分页
        Page<Book> bookPage = new Page<>(currentPage,pageSize);

        //查询条件
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.orderByDesc("buycount");

        Page<Book> bookPageResult = bookMapper.selectPage(bookPage, bookQueryWrapper);

        return bookPageResult;
    }


    @Override
    public Page<Book> getPageBooksByTypeId(Integer currentPage, Integer pageSize, Integer typeId){

        //分页条件
        Page<Book> bookPage = new Page<>(currentPage,pageSize);

        //查询条件
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.eq("typeId",typeId);

        Page<Book> bookPageResult = bookMapper.selectPage(bookPage, bookQueryWrapper);

        return bookPageResult;
    }

    @Override
    public Page<Book> searchBookBySql(Integer currentPage, Integer pageSize, String keyword) {

        Page<Book> page = new Page<>(currentPage,pageSize);

        //多字段模糊查询
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyword)
                .or()
                .like("provider",keyword)
                .or()
                .like("author",keyword)
                .or()
                .like("detail",keyword);

        Page<Book> bookPageResult = bookMapper.selectPage(page, queryWrapper);

        return bookPageResult;
    }


    @Override
    public Page<EsBook> searchBookByES(Integer currentPage, Integer pageSize, String keyword) {

        //构建查询条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        //query
        nativeSearchQueryBuilder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name", "typeName", "provider", "author", "detail"));

        //分页条件
        //注意: es中的分页是从第0页开始的
        nativeSearchQueryBuilder.withPageable(PageRequest.of(currentPage-1,pageSize));

        //排序
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("buycount").order(SortOrder.DESC));

        org.springframework.data.domain.Page<EsBook> esBookPage = esBookRepository.search(nativeSearchQueryBuilder.build());

        //将 Es的page 转换 mybatisPlus的 page
        Page<EsBook> page = new Page<>(currentPage,pageSize);
        page.setPages(esBookPage.getTotalPages());
        page.setTotal(esBookPage.getTotalElements());
        page.setRecords(esBookPage.getContent());

        return page;
    }

}
